package com.retail.proxy.service.impl;


import com.retail.proxy.entity.vo.ProxyMechanismCategoryVo;
import com.retail.proxy.entity.vo.ProxyUpgradeConfigVo;
import com.retail.core.entity.ParamReq;
import com.retail.proxy.entity.request.ProxyUpgradeConfigRequest;
import com.retail.proxy.mapper.ProxyMechanismCategoryMapper;
import com.retail.proxy.mapper.ProxyUpgradeConfigMapper;
import com.retail.proxy.service.ProxyUpgradeConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 晋升配置 Service impl
 *
 * @author Administrator
 */
@Service
public class ProxyUpgradeConfigServiceImpl implements ProxyUpgradeConfigService {

    @Resource
    private ProxyUpgradeConfigMapper proxyUpgradeConfigMapper;

    @Resource
    private ProxyMechanismCategoryMapper proxyMechanismCategoryMapper;

    @Override
    public ProxyUpgradeConfigVo getModel(Long id) {
        ProxyUpgradeConfigVo configVo = proxyUpgradeConfigMapper.getModel(id);
        ProxyMechanismCategoryVo oraVo = proxyMechanismCategoryMapper.getModel(configVo.getOraCategoryId());
        ProxyMechanismCategoryVo vo = proxyMechanismCategoryMapper.getModel(configVo.getCategoryId());
        configVo.setOraCategoryIdName(oraVo.getCategoryName());
        configVo.setCategoryIdName(vo.getCategoryName());
        return configVo;
    }

    @Transactional
    @Override
    public Long save(ProxyUpgradeConfigRequest proxyUpgradeConfigRequest) {
        proxyUpgradeConfigMapper.save(proxyUpgradeConfigRequest);
        return proxyUpgradeConfigRequest.getId();
    }

    @Transactional
    @Override
    public Long update(ProxyUpgradeConfigRequest proxyUpgradeConfigRequest) {
        proxyUpgradeConfigMapper.update(proxyUpgradeConfigRequest);
        return proxyUpgradeConfigRequest.getId();
    }

    @Override
    public List<ProxyUpgradeConfigVo> listPage(ParamReq<ProxyUpgradeConfigRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<ProxyUpgradeConfigVo> res = proxyUpgradeConfigMapper.listPage(params);
        res.forEach(r -> {
            ProxyMechanismCategoryVo oraVo = proxyMechanismCategoryMapper.getModel(r.getOraCategoryId());
            ProxyMechanismCategoryVo vo = proxyMechanismCategoryMapper.getModel(r.getCategoryId());
            r.setOraCategoryIdName(oraVo.getCategoryName());
            r.setCategoryIdName(vo.getCategoryName());
        });

        return res;
    }

    @Override
    public Integer countPage(ParamReq<ProxyUpgradeConfigRequest> params) {
        return proxyUpgradeConfigMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return proxyUpgradeConfigMapper.updateDelFlagById(id, delFlag);
    }
}
